/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ntb.models.impl;

import com.ntb.models.*;
import com.ntb.common.DatabaseConnection;
import com.ntb.entities.LandStatus;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author AnhTuan
 */
public class LandStatusDAOImpl extends DatabaseConnection implements LandStatusDAO {

    private List statusList;

    public LandStatusDAOImpl() {
        statusList = new ArrayList();
        statusList.add(new String[]{"CR", "Created"});
        statusList.add(new String[]{"AB", "Applied Building Permit"});
        statusList.add(new String[]{"RB", "Received Building Permit"});
        statusList.add(new String[]{"CS", "Constructed"});
        statusList.add(new String[]{"AO", "Applied Occupancy Permit"});
        statusList.add(new String[]{"RO", "Received Occupancy Permit"});
        statusList.add(new String[]{"BS", "To Be Sold"});
        statusList.add(new String[]{"S", "Sold"});

    }

    public Iterator<LandStatus> getList(int landID) {
        try {
            ArrayList list = new ArrayList();
            openConnection();
            ResultSet rs = getResultset("Select * From LandStatus WHERE LandID = " + landID + " ORDER BY ChangedDate DESC");
            while (rs.next()) {
                LandStatus row = new LandStatus();
                row.setLandID(rs.getInt("LandID"));
                String statusCode = rs.getString("StatusCode");
                row.setStatusCode(statusCode);
                row.setChangedDate(rs.getTimestamp("ChangedDate"));
                row.setStatusName(this.getStatusName(statusCode));
                
                list.add(row);
            }
            closeConnection();
            return list.iterator();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public List getStatusTable() {
        return this.statusList;
    }

    public String getStatusName(String statusCode) {
        for (Iterator it = this.statusList.iterator(); it.hasNext();) {
            String[] object = (String[]) it.next();
            if (object[0].equals(statusCode)) {
                return object[1];
            }
        }
        return null;

    }

    public int add(LandStatus object) {
        try {
            if (this.checkExist(object)) {
                return 0;
            } else {
                openConnection();
                PreparedStatement pst = conn.prepareStatement("Insert Into LandStatus(LandID,StatusCode,ChangedDate) Values(?,?,?)");
                pst.setInt(1, object.getLandID());
                pst.setString(2, object.getStatusCode());
                pst.setTimestamp(3, new Timestamp(new java.util.Date().getTime()));
                int result = pst.executeUpdate();
                closeConnection();
                return result;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return -1;
    }

    private boolean checkExist(LandStatus object) {
        try {
            openConnection();
            PreparedStatement pst = conn.prepareStatement("Select LandID From LandStatus Where LandID=? AND StatusCode=?");
            pst.setInt(1, object.getLandID());
            pst.setString(2, object.getStatusCode());
            ResultSet rs = pst.executeQuery();
            boolean result = rs.next();
            closeConnection();
            return result;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }

    }

    public int delete(int id) {
        try {
            openConnection();
            PreparedStatement pst = conn.prepareStatement("Delete LandStatus Where LandID=?");
            pst.setInt(1, id);
            int result = pst.executeUpdate();
            closeConnection();
            return result;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return -1;
    }
}
